<template>
	<view class="page">
		<!-- <view class="developing">
			正在开发当中，请尽情期待... ...
		</view> -->
		<view>
			<view class="item-back" >
			<!-- <view class="item-back"> -->
			<view class="back-select" v-for="(item,index) of bankWX"  v-if="item.bank_type == 1" >
				<view class="select-content">
					<image class="select-icon" v-if="item.bank_type == 3" src="../../../../static/bank.png"></image>
					<image class="select-icon" v-if="item.bank_type == 2" src="../../../../static/zfb1.png"></image>
					<image class="select-icon" v-if="item.bank_type == 1" src="../../../../static/wx1.png"></image>
					<view class="select-name">
						<text>{{item.name}}</text>
						<text v-if="item.bank_type == 1">微信</text>
						<text v-if="item.bank_type == 2">支付宝</text>
						<view class="item-delect">
							<text>{{item.thumb}}</text>
							<!-- <image @click="deleteCar(item.id)" src="../../../../static/delete.png"></image> -->
						</view>
					</view>
				</view>
			</view>
				<!-- <view class="back-select" >
					<text class="backSelect" v-if=" bank.type == '' ">请选择充值类型</text>
					<view>
						<image class="back-logo" v-if="bank.type == 3" src="../../../../static/bank.png"></image>
						<image class="back-logo" v-if="bank.type == 2" src="../../../../static/zfb1.png"></image>
						<image class="back-logo" v-if="bank.type == 1" src="../../../../static/wx1.png"></image>
					</view>
					<view class="back-describe">			
						<text class="back-name" v-if="bank.type == 1">微 信</text>
						<text class="back-name"  v-if=" bank.type == 2 ">支付宝</text>
						<text class="back-name"  v-if=" bank.type == 3 ">{{bank.name}}</text>
						<text class="back-tag">{{bank.binding}}</text>
					</view>
				</view> -->
<!-- 				<image class="back-select-next" src="../../../../static/jiantou3.png"></image>
 -->			</view>
			
			<view class="item-withdraw">
				<view class="item-name">充值金额</view>
				<view class="item-price">
					<view class="priceBox">
						<text>&yen</text>
						<input type="digit" focus="" v-model="price" />
					</view>
					<view class="tick" v-if="price != ''">
						<text v-if="price >99 &&price<999">/百</text>
						<text v-if="price >999 &&price<9999">/千</text>
						<text v-if="price >9999">/万</text>
						<view>×</view>
					</view>
					
				</view>
				<view class="line"></view>
				<view class="balance">
					<view>账户余额 {{nowprice}} 元</view>
				</view>
			</view>
			<!-- <view class="discribe">
				<text>备注:</text>
				<input type="text" v-model="bank_discribe"/>
			</view> -->
			<view class="confirm" @click="$noMultipleClicks(fromSubmit)">
				<text>确   定</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				noClick:true,
				bank: {
					name: '',
					icon: '',
					binding: '',
					id: '',
					type:'',
					bank_code:'',
					// name: '桂林银行',
					// icon: '../../../../static/guilLogo.jpg',
					// binding: '尾号6666储蓄卡',
					// id: '-1',
				},
				price:'',//提现的金额
				nowprice:'',//账户余额
				developing:false,
				timer:null,
				bankType:[],
				type_name:'',
				index: 0,
				bank_discribe:'',//备注
				bankWX:[],
			};
		},
		onLoad(option) {
			//this.getBankType();
			this.getData();
			this.getData2();
			if(option.select == 1 ) {
				console.log(option)
				this.bank.name = JSON.parse(option.bank).name;
				this.bank.type = JSON.parse(option.bank).bank_type;
				//console.log(this.bank.name)
				this.bank.bank_code = JSON.parse(option.bank).bank_code;
				this.bank.binding = JSON.parse(option.bank).thumb;
				this.bank.id = JSON.parse(option.bank).id;
			}						
		},
		methods: {	
			/*获取余额数据*/
			getData() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get(
					'user.index/my_money',
					{
						
					},
					function(data) {
						//console.log(data);
						self.tableData = data.data.list;
						self.nowprice = self.tableData.balance;
						//console.log(self.nowprice)
						//this.loading = true;
						uni.hideLoading();
					}
				);
			},
			/* 获取类型 */
			getBankType() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('home/bank_type', {}, function(res) {
					//console.log(res);
					self.bankType = res.data.type_list;
					uni.hideLoading();
				});
			},
			/*卡类型选择 */
			selectType: function(e) {
				console.log(e);
				let driverId = e.detail.value;
				let type = this.bankType[driverId];
				this.type_name = type.type_name;
				this.type_id = type.id;//卡类型id
				console.log(this.type_id)
			},
			closeIntegral() {
				this.developing = false
				if (this.timer) {
					clearTimeout(this.timer);
					this.timer = null;
				}
			},
			showIntegral() {
				this.developing = true;
				this.timer = setTimeout(() => {
					this.developing = false
				}, 3000);
			},
			/* 获取数据 */
			getData2() {
				let self = this;
				uni.showLoading({
					title: '加载中'
				});
				self._get('user.index/bankCard', {}, function(res) {
					//console.log(res);
					self.bankWX = res.data.bank;
					self.bankWX.forEach(item =>{
						if(item.bank_type==3){
							var tel = item.bank_code;
							    tel = "" + tel;
							    var newTel = tel.substr(0,4) + " **** **** **** " + tel.substr(15)
							   	item.thumb = newTel
								
						}else{
							var tel = item.bank_code;
							    tel = "" + tel;
							    var newTel = tel.substr(0,3) + " **** " + tel.substr(7)
							   	item.thumb = newTel
								
						}						
					})
					self.loadding = false;
					uni.hideLoading();
				});
			},
			fromSubmit(){
				let self = this;
				let formdata = {};
				
				// if(self.bank.type == ''){
				// 	uni.showModal({
				// 		title:'请选择充值类型',
				// 		duration:1000,
				// 		icon:'none'
				// 	});
				// 	return;
				// }
				if (self.price == '') {
					uni.showToast({
						title: '请请输入金额',
						duration: 1000,
						icon: 'none'
					});
					return false;
				}
				// if (self.price > self.nowprice) {
				// 	uni.showToast({
				// 		title: '余额不足',
				// 		duration: 1000,
				// 		icon: 'none'
				// 	});
				// 	return false;
				// }
				//formdata.name = self.bank.name;
				//formdata.bank_type = self.bank.type;
				//formdata.bank_id = self.bank.id;
				formdata.money = self.price;
				//formdata.describe = self.bank_discribe;
				uni.showLoading({
					title: '正在提交'
				});
				
				//plus.runtime.openURL('https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx28110304605565db846dc78c7c463e0000&package=637396268');
				//self.gotoPage('https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx281036391966339b151bc6b3ec7c9a0000&package=1107321972')
				//window.location.href = "https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx281036391966339b151bc6b3ec7c9a0000&package=1107321972"
				self._post(
					'user.index/recharge',
					formdata,
					result => {
						// 记录token user_id
						// uni.setStorageSync('token', result.data.token);
						// uni.setStorageSync( 'user_id', result.data.user_id );
						
						if (result.code == 1) {
							let orderInfo = JSON.stringify(result.data.pay_info);
							uni.requestPayment({
								"provider": "wxpay",
								//"orderInfo":orderInfo,
								//"orderInfo": result.data.pay_info,
								"orderInfo": {
									"appid": result.data.pay_info.appid,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
									"noncestr": result.data.pay_info.noncestr, // 随机字符串
									"package": "Sign=WXPay",        // 固定值
									"partnerid": result.data.pay_info.partnerid ,     // 微信支付商户号
									"prepayid": result.data.pay_info.prepayid, // 统一下单订单号 
									"timestamp": result.data.pay_info.timestamp,        // 时间戳（单位：秒）
									"sign": result.data.pay_info.sige // 签名，这里用的 MD5 签名
								},
								fail: function(err) {
									uni.showToast({
										title: err.errMsg,
										icon: "none"
									})
									console.log(err)
								},
								success: function(res) {
									uni.showToast({
										title: "充值成功",
										icon: "none"
									});
									console.log(res)
								},														
							})
							//console.log(result)
							//plus.runtime.openURL(result.mweb_url);
							//const win = plus.webview.create(result.mweb_url, "test", {additionalHttpHeaders: { Referer: "http://lianhela.ttjxw.com" },}); 
							//win.show();
							// uni.showToast({
							// 	title: '充值成功！',
							// 	duration: 2000,
							// 	icon: 'none'
							// });
							
							//self.gotoPage('/pagesUser/user/my-wallet/my-wallet');
						}
						// 执行回调函数
						// uni.navigateBack();
					},
					false,
					() => {
						uni.hideLoading();
					}
				);
			},
			/*跳转页面*/
			gotoPage(path) {
				let self = this;
				if (path.startsWith('/')) {
					uni.navigateTo({
						url: path
					});
				} else {
					self[path]();
				}
			},
		}
	}
</script>

<style lang="scss">
	@import 'skype.scss';
.priceBox,.tick{
	display: flex;
	align-items: center;
}
	.tick text{
		font-size: 18rpx;
		color: #999999;
	}
	.tick{
		margin-right: 36rpx;
	}
	.tick view{
		width: 24rpx;
		background-color: #DDDDDD;
		height: 24rpx;
		line-height: 24rpx;
		text-align: center;
		color: #ffffff;
		border-radius: 50%;
	}
</style>
